<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            width:100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box{
            position: relative;
            background-image: url("1.jpeg");
            width: 400px;
            height: 300px;
            background-size: 100% 100%;
        }
        .move{
            display: block;
            position: absolute;
            display: none;
            width: 200px;
            height: 200px;
            border-radius: 50%;
            cursor: move;
            background-image: url("1.jpeg");
        }
    </style>
</head>
<body>
    <div class="box" onmouseenter="show(event)">
        <div class="box">
            <div class="move">
            </div>
        </div>
    <script>
    
            var box = document.getElementsByClassName("box")[0];
            var move = document.getElementsByClassName("move")[0];

            var w = parseInt(window.getComputedStyle(move).width);
    
            var h = parseInt(window.getComputedStyle(move).height);
    
            console.log(box.offsetLeft);
            box.onmouseenter = function (event){
                //放大镜图像的位置
                var left = event.clientX - w/2 - box.offsetLeft;
                var top = event.clientY - h/2 - box.offsetHeight;
                move.style.display = "block";
                move.style.left = left+"px";
                move.style.top = top+"px";            
                move.onmousemove = function(event){
                    var left = event.clientX - box.offsetLeft;
                    var top = event.clientY  - box.offsetHeight;
                    move.style.display = "block";
                    move.style.left = left-w/2+"px";
                    move.style.top = top-h/2+"px";
    
                    move.style.backgroundSize = box.offsetWidth * 2+"px" + " "+box.offsetHeight * 2 + "px";
    
                    move.style.backgroundPositionX = -(left * 2 - w/2) + "px";
                    move.style.backgroundPositionY = -(top * 2 - h/2) + "px";
    
                    if(top < 0 || left < 0 || left > 400 || top > 300){
                        move.style.display = "none";
                    }
                }
                
            }
    </script>
</body>
</html>